package database;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Update extends JInternalFrame {
	
	Connection conn;
	DatabaseTest b;
	
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JTextField code;
	private JTextField ea;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JLabel status;
	private JLabel lblSerial;
	private JTextField serial;

	public Update(Connection c, DatabaseTest b) {
		
		super("수정", true, true, true, true);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);  // 창을 끄고 메인에서 다시 선택했을때 뜨게하는것
		setBounds(100, 100, 475, 245);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getBtnNewButton_1());
		getContentPane().add(getStatus());
		getContentPane().add(getLblSerial());
		getContentPane().add(getSerial());
		
		this.conn = c;
		this.b = b;
	}
	
	public void setData(String s, String c, String e){
		serial.setText(s);
		code.setText(c);
		ea.setText(e);
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("code");
			lblNewLabel.setFont(new Font("나눔고딕", Font.PLAIN, 12));
			lblNewLabel.setBounds(66, 68, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("ea");
			lblNewLabel_1.setFont(new Font("나눔고딕", Font.PLAIN, 12));
			lblNewLabel_1.setBounds(66, 101, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(127, 64, 173, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setBounds(127, 97, 173, 21);
			ea.setColumns(10);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s = Integer.parseInt(serial.getText());
					String sql = "select * from ptbl where serial = ?";
					
					try {
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setInt(1, s);
						
						// select 된 값들이 모두 rs에 들어있다. 
						ResultSet rs = pstmt.executeQuery();
						if(rs.next()){
							code.setText(rs.getString("code"));
							ea.setText("" + rs.getInt("ea"));
							status.setText(serial.getText() + "이 검색되었습니다");
						}else{
							JOptionPane.showMessageDialog(null, "없음");
							status.setText(serial.getText() + "는 존재하지 않는 데이터입니다.");
							code.setText("");
							ea.setText("");
							serial.requestFocus();
						}
						
						rs.close();
						pstmt.close();
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			});
			btnNewButton.setFont(new Font("나눔고딕", Font.PLAIN, 12));
			btnNewButton.setBounds(334, 33, 97, 23);
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("수정");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s = Integer.parseInt(serial.getText());
					String c = code.getText();
					int e1 = Integer.parseInt(ea.getText());
					
					String sql = "update ptbl set code = ?, ea = ? where serial = ?";
					
					try {
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setInt(2, e1);
						pstmt.setInt(3, s);
						
						pstmt.executeUpdate();
						pstmt.close();
						
					} catch (Exception ex) {
						ex.printStackTrace();
					}
					
					b.select.setList();
				}
			});
			btnNewButton_1.setFont(new Font("나눔고딕", Font.PLAIN, 12));
			btnNewButton_1.setBounds(127,  128, 97, 30);
		}
		return btnNewButton_1;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("검색할 코드를 입력하세요");
			status.setBounds(12, 180, 558, 15);
		}
		return status;
	}
	private JLabel getLblSerial() {
		if (lblSerial == null) {
			lblSerial = new JLabel("serial");
			lblSerial.setFont(new Font("나눔고딕", Font.PLAIN, 12));
			lblSerial.setBounds(66, 37, 57, 15);
		}
		return lblSerial;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(127, 33, 173, 21);
		}
		return serial;
	}
}
